package com.huizhonglm.diap.presenters.tab;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.category.CategoryInfo;
import com.huizhonglm.diap.api.model.category.CategoryParams;
import com.huizhonglm.diap.api.model.shop.ShopInfo;
import com.huizhonglm.diap.api.model.shop.ShopListParams;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.location.LocationUtil;
import com.huizhonglm.diap.core.storage.model.LocationInfo;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.tab.iView.TabhostNearIView;
import com.huizhonglm.diap.widget.expandTab.MenuData;
import com.huizhonglm.diap.widget.expandTab.MenuInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:53
 */
public class TabhostNearPresenter extends BasePresenter {
    public static final String EX_TAB_CATEGORY = "category";
    public static final String EX_TAB_RADIUS = "radius";
    public static final String EX_TAB_SMARTSORT = "sort";
    public static final String EX_TAB_FILTER = "filter";

    private TabhostNearIView iView;

    private int pageIndex;

    public TabhostNearPresenter(TabhostNearIView iView) {
        this.iView = iView;
    }

    public void loadData(final String pullFlag, String isParent, String category, double radius, String sort, String filter){
        ShopListParams params = new ShopListParams(API.buildUri(API.NEAR_SHOP_LIST));
        switch (pullFlag) {
            case Constants.LOAD_INIT:
            case Constants.LOAD_DROP:
                pageIndex = 1;
                break;
            case Constants.LOAD_MORE:
                pageIndex ++;
                break;
        }
        LocationInfo loc = LocationUtil.getLocation();
        if (loc != null) {
            params.lat = loc.getLatitude();
            params.lng = loc.getLongitude();
        } else {
            params.lat = Constants.LATITUDE;
            params.lng = Constants.LONGITUDE;
        }

        params.radius = radius;
        params.isPar = isParent;
        params.categoryId = category;
        params.sortId = sort;
        params.filterId = filter;
        params.pageNumber = pageIndex;
        API.http().post("nearShopList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<ShopInfo> data = JSON.parseArray(result, ShopInfo.class);
                    if (!EmptyUtil.isEmpty(data)) {
                        iView.onLoadDataSuccess(pullFlag, data);
                    } else {
                        iView.onLoadDataEmpty(pullFlag);
                    }
                } else {
                    iView.onLoadDataEmpty(pullFlag);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadDataFailure(pullFlag);
            }
        });
    }

    public void loadMenu(){
        API.http().post("categoryList", new CategoryParams(), new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<CategoryInfo> list = JSON.parseArray(result, CategoryInfo.class);
                    MenuData menuData = new MenuData();
                    menuData.setCategoryData(createCategory(list));
                    menuData.setSmartSortData(createSmartSort());
                    menuData.setFilterData(createFilter());
                    menuData.setRadiusData(createRadius());
                    iView.onLoadMenuSuccess(menuData);
                } else {
                    iView.onLoadMenuSuccess(null);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadMenuFailure();
            }
        });
    }

    private List<MenuInfo> createCategory(List<CategoryInfo> list){
        List<MenuInfo> data = new ArrayList<>();
        for (CategoryInfo info : list) {
            List<MenuInfo> children = new ArrayList<>();
            MenuInfo all = new MenuInfo();
            all.setId(info.getSbId());
            all.setName("全部");
            all.setAmount(0);
            all.setType(EX_TAB_CATEGORY);
            all.setIsParent("Y");
            children.add(all);
            if (!EmptyUtil.isEmpty(info.getChildren())){
                for (CategoryInfo child : info.getChildren()) {
                    MenuInfo ci = new MenuInfo();
                    ci.setId(child.getSbId());
                    ci.setName(child.getBussName());
                    ci.setAmount(child.getAmount());
                    ci.setType(EX_TAB_CATEGORY);
                    ci.setIsParent("N");
                    children.add(ci);
                }
            }
            MenuInfo mi = new MenuInfo();
            mi.setId(info.getSbId());
            mi.setName(info.getBussName());
            mi.setType(EX_TAB_CATEGORY);
            mi.setIsParent("Y");
            mi.setChildren(children);
            data.add(mi);
        }
        return data;
    }

    private List<MenuInfo> createRadius() {
        List<MenuInfo> radius = new ArrayList<>();

        MenuInfo all = new MenuInfo();
        all.setId("-9");
        all.setName("全城");
        all.setType(EX_TAB_RADIUS);
        all.setIsParent("Y");
        MenuInfo m500 = new MenuInfo();
        m500.setId("500");
        m500.setName("500米");
        m500.setType(EX_TAB_RADIUS);
        m500.setIsParent("Y");
        MenuInfo m1000 = new MenuInfo();
        m1000.setId("1000");
        m1000.setName("1千米");
        m1000.setType(EX_TAB_RADIUS);
        m1000.setIsParent("Y");
        MenuInfo m2000 = new MenuInfo();
        m2000.setId("2000");
        m2000.setName("2千米");
        m2000.setType(EX_TAB_RADIUS);
        m2000.setIsParent("Y");
        MenuInfo m3000 = new MenuInfo();
        m3000.setId("3000");
        m3000.setName("3千米");
        m3000.setType(EX_TAB_RADIUS);
        m3000.setIsParent("Y");
        MenuInfo m4000 = new MenuInfo();
        m4000.setId("4000");
        m4000.setName("4千米");
        m4000.setType(EX_TAB_RADIUS);
        m4000.setIsParent("Y");
        MenuInfo m5000 = new MenuInfo();
        m5000.setId("5000");
        m5000.setName("5千米");
        m5000.setType(EX_TAB_RADIUS);
        m5000.setIsParent("Y");
        radius.add(all);
        radius.add(m500);
        radius.add(m1000);
        radius.add(m2000);
        radius.add(m3000);
        radius.add(m4000);
        radius.add(m5000);
        return radius;
    }

    private List<MenuInfo> createSmartSort(){
//      sortId = 1 智能排序
//      sortId = 2 距离优先
//      sortId = 3 最新发布
        List<MenuInfo> sort = new ArrayList<>();
        MenuInfo sm1 = new MenuInfo();
        sm1.setId("1");
        sm1.setName("智能排序");
        sm1.setType(EX_TAB_SMARTSORT);
        sm1.setIsParent("Y");
        MenuInfo sm2 = new MenuInfo();
        sm2.setId("2");
        sm2.setName("距离最近");
        sm2.setType(EX_TAB_SMARTSORT);
        sm2.setIsParent("Y");
        MenuInfo sm3 = new MenuInfo();
        sm3.setId("3");
        sm3.setName("最新发布");
        sm3.setType(EX_TAB_SMARTSORT);
        sm3.setIsParent("Y");
        sort.add(sm1);
        sort.add(sm2);
        sort.add(sm3);
        return sort;
    }

    private List<MenuInfo> createFilter(){
//        filterId = 1 不限
//        filterId = 2 小于50
//        filterId = 3 50-100
//        filterId = 4 100-200
//        filterId = 5 200-300
//        filterId = 6 大于300
        List<MenuInfo> filter = new ArrayList<>();
        MenuInfo fm1 = new MenuInfo();
        fm1.setId("1");
        fm1.setName("不限");
        fm1.setType(EX_TAB_FILTER);
        fm1.setIsParent("Y");
        filter.add(fm1);
        MenuInfo fm2 = new MenuInfo();
        fm2.setId("2");
        fm2.setName("50以下");
        fm2.setType(EX_TAB_FILTER);
        fm2.setIsParent("Y");
        filter.add(fm2);
        MenuInfo fm3 = new MenuInfo();
        fm3.setId("3");
        fm3.setName("50-100");
        fm3.setType(EX_TAB_FILTER);
        fm3.setIsParent("Y");
        filter.add(fm3);
        MenuInfo fm4 = new MenuInfo();
        fm4.setId("4");
        fm4.setName("100-200");
        fm4.setType(EX_TAB_FILTER);
        fm4.setIsParent("Y");
        filter.add(fm4);
        MenuInfo fm5 = new MenuInfo();
        fm5.setId("5");
        fm5.setName("200-300");
        fm5.setType(EX_TAB_FILTER);
        fm5.setIsParent("Y");
        filter.add(fm5);
        MenuInfo fm6 = new MenuInfo();
        fm6.setId("6");
        fm6.setName("300以上");
        fm6.setType(EX_TAB_FILTER);
        fm6.setIsParent("Y");
        filter.add(fm6);
        return filter;
    }
}
